package com.caac.core.dao.hibernate;

import java.util.List;

import com.caac.core.dao.PageCounterDao;
import com.caac.core.dm.PageCounter;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2004</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public class PageCounterDaoHibernate extends BaseDaoHibernate implements
        PageCounterDao {
    public PageCounter getPageCounter(String servletPath) {
        List counters = getHibernateTemplate().find(
                "from PageCounter pc where pc.servletPath = ?", servletPath);
        if (counters == null || counters.size() == 0) {
            return null;
        }
        return (PageCounter) counters.get(0);
    }

    public List getAllPageCounters() {
        return getHibernateTemplate().find(
                "from PageCounter pc where pc.ifStatics = ? ", "1");
    }

    public int getSumPageCounter() {
        return ((Integer) getHibernateTemplate().iterate(
                "select sum(pc.counter) from PageCounter pc where pc.ifStatics = ?",
                "1").next()).intValue();
    }

    public void savePageCounter(PageCounter counter) {
        getHibernateTemplate().saveOrUpdate(counter);
    }

    public void savePageCounter(String pagePath) {
        this.execNoResultSql(
                "UPDATE j42 SET visit_count=visit_count+1 WHERE url='" +
                pagePath + "'");
    }

    private String GBKToIso(String strIn) {
        String strOut = "";
        if (strIn != null) {
            try {
                strOut = new String(strIn.trim().getBytes("GBK"),
                                    "ISO8859_1");
            } catch (Exception e) {}
        }
        return strOut;
    }
}
